package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Option;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.Section;
import com.sl.au.entity.SectionArea;
import com.sl.au.entity.User;
import com.sl.au.service.SaleAreaService;
import com.sl.au.service.SectionService;
import com.sl.au.service.UserService;

@RestController
@RequestMapping(value = "/ui/section")
public class SectionController extends BaseController {

	@Autowired
	SectionService sectionService;
	
	@Autowired
	UserService userService;

	@Autowired
	SaleAreaService saleAreaService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("section/section-index");
		String superior = "上级部门";
		List<Option> superiors = new ArrayList<Option>();
		superiors = saleAreaService.getRepository().findByGroup(superior);
		modelAndView.addObject("superiors", superiors);
		
		List<User> chiefs=userService.getRepository().findAll();
		modelAndView.addObject("chiefs", chiefs);
		return modelAndView;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<Section> sections = null;
		sections = sectionService.getPage(start, length, key);
		long count = sectionService.getRepository().getCount(key);
		map.put("data", sections);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	////保存科室
	@RequestMapping(value = "/saveSection", method = RequestMethod.POST)
	public Map<String, Object> saveSection(HttpServletRequest request,
			@RequestBody Section section) {
		Map<String, Object> map = new HashMap<String, Object>();
		sectionService.getRepository().save(section);
		map.put("code", 0);
		return map;
	}
	
	///编辑保存科室
	@RequestMapping(method = RequestMethod.PATCH)
	public Map<String, Object> patchSection(@RequestBody Section section) {
		Map<String, Object> map = new HashMap<String, Object>();
		sectionService.patch(section);	
		map.put("code", 0);
		return map;
	}

	// 删除科级
	@RequestMapping(value = "/deleteSection", method = RequestMethod.GET)
	public Map<String, Object> delBid(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String id[] = ids.split(",");
		List<Section> sections = new ArrayList<Section>();
		for (int i = 0; i < id.length; i++) {
			String sectionId = id[i];
			Section s = new Section();
			s = sectionService.getRepository().findOne(sectionId);
			
			if (s != null) {
				List<SectionArea> sa=sectionService.getSARepository().findBySection(s);
				sectionService.getSARepository().deleteInBatch(sa);
			}
			sections.add(s);
		}
		sectionService.getRepository().deleteInBatch(sections);
		map.put("code", 0);
		return map;
	}

	/// 部门下的科室是否重复
	@RequestMapping(value = "namevalidate", method = RequestMethod.GET)
	public Map<String, Object> codeValidate(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		long count = 0;
		String sectionArea = request.getParameter("sectionArea");
		String sectionId = request.getParameter("id");
		if (sectionId == null || sectionId.equals("")) {
			count = sectionService.getRepository().getRearea(sectionArea);
		} else {
			Section section = sectionService.getRepository().findBySectionArea(sectionArea);
			if (section != null) {
				if (!section.getId().equals(sectionId)) {
					count = 1;
				}
			}
		}
		map.put("code", count);
		return map;
	}

	///// 获得科室已经配置的片区///////
	@RequestMapping(value = "getselectpage", method = RequestMethod.GET)
	public Map<String, Object> getSelectPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String sectionId = request.getParameter("sectionId");
		if(sectionId==null){
			sectionId="";
		}
		List<SectionArea> sectionAreas = null;
		long count = 0;
		sectionAreas = sectionService.findSectionArea(start, length, sectionId);
		count = sectionService.getRepository().getSACount(sectionId);
		map.put("data", sectionAreas);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	//// 获得科室没有配置的片区////////
	@RequestMapping(value = "getnoselectpage", method = RequestMethod.GET)
	public Map<String, Object> getNoSelectPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String sectionId = request.getParameter("sectionId");
		String superior=request.getParameter("superior");
		List<SaleArea> noSelectAreas = null;
		long count = 0;
		if (sectionId == null) {
			sectionId="";
		}
		if (superior == null) {
			superior="";
		}
	    noSelectAreas = sectionService.findNoSArea(start, length, sectionId,superior);
		count = sectionService.getRepository().getNoACount(sectionId,superior);
		map.put("data", noSelectAreas);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 科室配置片区
	 * 
	 * @return
	 */
	@RequestMapping(value = "/addArea", method = RequestMethod.POST)
	public Map<String, Object> TicketSend(HttpServletRequest request,
			@RequestBody SectionArea sectionArea) {
		Map<String, Object> map = new HashMap<String, Object>();
		sectionService.getSARepository().save(sectionArea);
		map.put("code", 0);
		return map;
	}

}
